#ifndef BLOG_HOME_PAGE_H
#define BLOG_HOME_PAGE_H
extern "C"{
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
}

#include <vector>
#include <string>
#include <functional>
#include <map>
#include <iostream>

using namespace std;

typedef struct blog_file_time_s blog_file_time_t;

//blog_file modified time
struct blog_file_time_s{
 string blog_file_name;
 string blog_file_name_with_dir;
 long modify_time;
};

typedef struct blog_file_attr_s blog_file_attr_t;

//blog file attribute
struct blog_file_attr_s{
    blog_file_time_t blog_file_time;
    string author;
    string date;
    long date_secs;
    string month;
    long month_secs;
    string title;
    string tags;
    string summary;
    string stick; //just a "true" or "false" string
};

typedef struct blog_attr_s blog_attr_t;

struct blog_attr_s
{
    string blog_name;
    string blog_sub_name;
};

string strim_string(string before);


bool is_md_file(string name);

bool create_dir(string dir_name);


class blog_home_page
{
public:
    blog_home_page();
    ~blog_home_page();
    //open blog file dir,get all blog files' names
    //parameter:
    //blog_dir is the directory of the blog files
    //return:
    //open the directory wrong ,return false,or else return true
    bool get_blog_dir(string blog_dir);

    //open blog subdir and get the all sub dir's file names
    bool get_blog_subdir(string blog_dir);

    //get all blog_files' attribute,stored into the blog_file_attr
    //return:
    //stored successfully return true,else return false
    bool get_blog_files_attr();

    //open a blog file,get blog file attribute
    //parameters:
    //blog_file_attr is the blog file attribute
    //blog_file is the blog file's name
    bool get_a_blog_file_attr(blog_file_attr_t* blog_file_attr,string blog_file);

    //create index.html
    bool create_blog_index_page();
    bool create_blog_archive_page();
    bool create_blog_tags_page();

    bool create_blog_header(string html_title,string blog_name,string blog_subname);
    bool create_blog_left_sidebar();
    bool create_blog_right_sidebar();
    bool create_blog_ending();
    bool create_blog_index_contents(int max_items_per_page);
    bool create_blog_archive_contents(int max_items_per_page);
    bool create_blog_tags_contents(int max_items_per_page);

    bool create_blog_item(blog_file_attr_t bfa);
    //page_type contained index,archive,tag
    bool create_blog_pagination(int pages,string page_type,int max_item_per_page);
    bool create_archive_tags_blog_pagination(int pages, string page_type,
                                            int max_pages,string arch_tags);

    //create blog contents header
    bool create_md_header(string html_title,string blog_name,
                          string blog_subname,blog_file_attr_t bfa);
    //create contents and catalog
    bool create_md_contens(blog_file_attr_t bfa);

    //clear all variable
    bool clear_all();



private:
    //descending by month_secs (type is long) multimap
    //long is month_secs
    multimap<long,blog_file_attr_t,greater<long>> month_secs_mmap_blog_file_attr;
    //descending by date_secs (type is long) multimap
    //long is date_secs
    multimap<long,blog_file_attr_t,greater<long>> date_secs_mmap_blog_file_attr;

   //get file attribute loop MAX_TIMES
    int MAX_TIMES = 15;

    string html_header;
    string html_left_sidebar;
    string html_item;
    string html_pagination;

    vector<string>html_index_contents_v;
    map<string,string,less<string> > html_archive_contents_vv;
    map<string,string,less<string> > html_tags_contents_vv;


public:
    //blog_files is the vector store all blog file names in a blog dir
    vector<string> blog_files_without_dir;
    vector<string> blog_files;
    vector<string> blog_files_subdir;
    vector<string> html_index_page_v;
    map<string,string,less<string> >html_archive_page_vv;
    map<string,string,less<string> >html_tags_page_vv;

    //string is tags
    multimap<string,blog_file_attr_t,less<string>> tags_mmap_blog_file_attr;

    string html_md_contents;
    string html_md_catalog;
    string html_md_header;
    string html_right_sidebar;
    string html_ending;

};

#endif // BLOG_HOME_PAGE_H
